﻿Imports System.IO
Imports System.Drawing.Imaging
Public Class ViewEmp
    Public id As Integer
    Dim dt As New DataTable
    Private Sub ViewEmp_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Button4.Visible = False
        txtname.Focus()
        Dim frm = MainMenu
        'MessageBox.Show(frm.per)
        If frm.per <> 1 Then
            Button1.Visible = False
            btnadd.Visible = False
            Button2.Visible = False
            btnpathdate.Visible = False
            btnpathp.Visible = False
            btnpathr.Visible = False
            btnretire.Visible = False
        End If
        dtppos.Format = DateTimePickerFormat.Custom
        dtppos.CustomFormat = "dd-MMM-yyyy"
        dtprole.Format = DateTimePickerFormat.Custom
        dtprole.CustomFormat = "dd-MMM-yyyy"
        dtpdatein.Format = DateTimePickerFormat.Custom
        dtpdatein.CustomFormat = "dd-MMM-yyyy"

        txtpathdate.Visible = False
        txtpathp.Visible = False
        txtpathr.Visible = False
        txtval.Visible = False
        txtval2.Visible = False
        txtval3.Visible = False

        loadPosi()
        loadrole()
        loadsex()
        loadministry()
        loaddata()

        loadyearpos()
        loadyearrole()
        loadage()
        loadkid()
        btnretire.Visible = False
    End Sub
    Private Sub loadyearpos()
        If IsDBNull(dt.Rows(0).Item(11)) Or IsDBNull(dt.Rows(0).Item(12)) Then
            Exit Sub
        End If

        Dim dt1 As Date = dt.Rows(0).Item(12)
        Dim dt2 As Date = Now.ToShortDateString
        Dim dt3 As TimeSpan = (dt2 - dt1)
        Dim diff As Double = dt3.Days
        Dim sAge As String

        sAge = Str(Int(diff / 365)) & " ឆ្នាំ "
        diff = diff Mod 365
        sAge = sAge & Str(Int(diff / 30)) & " ខែ"
        'diff = diff Mod 30
        'sAge = sAge & Str(diff) & " Day(s)"
        lblpos.Text = sAge
    End Sub
    Private Sub loadkid()
        Dim ti = Now.Year
        Dim kid = cmdp.ExecuteScalar("SELECT COUNT(*) FROM [Relative] WHERE EmpId=" & id & " AND active=true AND TypeId=2")
        Dim overkid = cmdp.ExecuteScalar("SELECT COUNT(*) FROM [Relative] WHERE EmpId=" & id & " AND " & ti & "-year(RelateDOB) >=18 AND active=true AND TypeId=2")
        lblkid.Text = kid
        lbloverkid.Text = overkid
    End Sub
    Private Sub loadyearrole()
        If IsDBNull(dt.Rows(0).Item(14)) Or IsDBNull(dt.Rows(0).Item(15)) Then
            Exit Sub
        End If

        Dim dt1 As Date = dt.Rows(0).Item(15)
        Dim dt2 As Date = Now.ToShortDateString
        Dim dt3 As TimeSpan = (dt2 - dt1)
        Dim diff As Double = dt3.Days
        Dim sAge As String

        sAge = Str(Int(diff / 365)) & " ឆ្នាំ "
        diff = diff Mod 365
        sAge = sAge & Str(Int(diff / 30)) & " ខែ"
        'diff = diff Mod 30
        'sAge = sAge & Str(diff) & " Day(s)"
        lblrole.Text = sAge
    End Sub

    Private Sub loadage()
        Dim dt1 As Date = dt.Rows(0).Item(4)
        Dim dt2 As Date = Now.ToShortDateString
        Dim dt3 As TimeSpan = (dt2 - dt1)
        Dim diff As Integer = dt3.Days
        Dim age As String
        age = Str(Int(diff / 365)) & " ឆ្នាំ "
        lblage.Text = age
    End Sub

    Private Sub loaddata()
        cmdp.Parameters("@id") = id
        dt = cmdp.ExecuteDataTable("SELECT * FROM Employee WHERE EmpId=@id")
        txtname.Text = dt.Rows(0).Item("EmpName")
        cbosex.SelectedValue = dt.Rows(0).Item("SexId")
        txtno.Text = dt.Rows(0).Item("EmpNo")
        dtpdob.Value = dt.Rows(0).Item("EmpDOB")
        cbominis.SelectedValue = dt.Rows(0).Item("MinistryId")
        txtpob.Text = dt.Rows(0).Item("EmpPOB")
        txtphone.Text = dt.Rows(0).Item("EmpPhone").ToString
        txtknowledge.Text = dt.Rows(0).Item("EmpKnow")
        txtlanguage.Text = dt.Rows(0).Item("EmpFLang")
        cbopos.SelectedValue = dt.Rows(0).Item("PositionId")
        cborole.SelectedValue = dt.Rows(0).Item("RoleId")


        If IsDBNull(dt.Rows(0).Item("Pno")) Or IsDBNull(dt.Rows(0).Item("Pdate")) Then
            dtppos.Visible = False
            txtnopos.Visible = False
            cbpos.Checked = True
        Else
            txtnopos.Text = dt.Rows(0).Item("Pno")
            dtppos.Value = dt.Rows(0).Item("Pdate")
        End If

        'txtnopos.Text = dt.Rows(0).Item(10)
        'dtppos.Value = dt.Rows(0).Item(11)

        If IsDBNull(dt.Rows(0).Item("Rno")) Or IsDBNull(dt.Rows(0).Item("Rdate")) Then
            dtprole.Visible = False
            txtnorole.Visible = False
            cbrol.Checked = True
        Else
            txtnorole.Text = dt.Rows(0).Item("Rno")
            dtprole.Value = dt.Rows(0).Item("RDate")
        End If

        'txtnorole.Text = dt.Rows(0).Item(13)
        'dtprole.Value = dt.Rows(0).Item(14)

        txtcheck.Text = dt.Rows(0).Item("CheckNo")
        If IsDBNull(dt.Rows(0).Item("DateIn")) Then
            dtpdatein.Visible = False
            cbdatein.Checked = True
        Else
            dtpdatein.Value = dt.Rows(0).Item("DateIn")
        End If

        txttransfer.Text = dt.Rows(0).Item("Transfer")
        txtaddress.Text = dt.Rows(0).Item("Address")
        txtdetail.Text = dt.Rows(0).Item("Detail")
        txtpathp.Text = dt.Rows(0).Item("PosPdf").ToString
        txtpathr.Text = dt.Rows(0).Item("RolePdf").ToString
        txtpathdate.Text = dt.Rows(0).Item("dateinpdf").ToString
        txtfault.Text = dt.Rows(0).Item("Fault").ToString

        Try
            If IsDBNull(dt.Rows(0).Item("Photo")) Then
                MessageBox.Show("No Image")
            Else
                Dim bytImage() As Byte
                bytImage = CType(dt.Rows(0).Item("Photo"), Byte())
                Dim ms As New System.IO.MemoryStream(bytImage)
                Dim bmImage As New Bitmap(ms)
                ms.Close()
                PictureBox1.Image = bmImage
                PictureBox1.Refresh()
            End If
        Catch ex As Exception

        End Try

    End Sub
    Private Sub loadsex()
        cbosex.ValueMember = "SexId"
        cbosex.DisplayMember = "SexName"
        cbosex.DataSource = cmdp.ExecuteDataTable("SELECT SexId,SexName FROM Sex")
    End Sub

    Private Sub loadministry()
        cbominis.ValueMember = "MinistryId"
        cbominis.DisplayMember = "MinistryName"
        cbominis.DataSource = cmd.ExcecuteDatatable("SELECT MinistryId,MinistryName FROM Ministry")
    End Sub

    Private Sub loadPosi()
        cbopos.ValueMember = "PositionId"
        cbopos.DisplayMember = "PositionName"
        cbopos.DataSource = cmd.ExcecuteDatatable("SELECT PositionId,PositionName FROM [Position]")
    End Sub
    Private Sub loadrole()
        cborole.ValueMember = "RoleId"
        cborole.DisplayMember = "RoleName"
        cborole.DataSource = cmd.ExcecuteDatatable("SELECT RoleId,RoleName FROM Role")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'MessageBox.Show("មិនអាចសំណើរការបានទេព្រោះមិនទាន់សរសេរកូដ")
        If txtname.Text.Trim = "" Then
            MessageBox.Show("សូមបញ្ចូលឈ្មោះមន្រ្តីឱ្យបានត្រឹមត្រូវ")
            txtname.Focus()
            Exit Sub
        End If
        If txtno.Text.Trim = "" Then
            MessageBox.Show("សូមបញ្ចូលអត្តលេខឱ្យបានត្រឹមត្រូវ")
            txtno.Focus()
            Exit Sub
        End If
        If txtpob.Text.Trim = "" Then
            MessageBox.Show("សូមបញ្ចូលទីកន្លែងកមណើតឱ្យបានត្រឹមត្រូវ")
            txtpob.Focus()
            Exit Sub
        End If
        If txtknowledge.Text.Trim = "" Then
            MessageBox.Show("សូមបញ្ចូលកំរិតវប្បធម៌ឱ្យបានត្រឹមត្រូវ")
            txtknowledge.Focus()
            Exit Sub
        End If
        If txtlanguage.Text.Trim = "" Then
            MessageBox.Show("សូមបញ្ចូលភាសាឱ្យបានត្រឹមត្រូវ")
            txtlanguage.Focus()
            Exit Sub
        End If
        If txtnopos.Text.Trim = "" Then
            If cbpos.Checked = False Then
                MessageBox.Show("សូមបញ្ចូលលេខឋានន្តរស័ក្តិឱ្យបានត្រឹមត្រូវ")
                txtnopos.Focus()
                Exit Sub
            End If
        End If
        If txtnorole.Text.Trim = "" Then
            If cbrol.Checked = False Then
                MessageBox.Show("សូមបញ្ចូលលេខតួនាទីឱ្យបានត្រឹមត្រូវ")
                txtnorole.Focus()
                Exit Sub
            End If
        End If
        If txtcheck.Text.Trim = "" Then
            MessageBox.Show("សូមបញ្ចូលលេខបណ្ណ័ត្រួតពិនិត្រឱ្យបានត្រឹមត្រូវ")
            txtcheck.Focus()
            Exit Sub
        End If
        If txttransfer.Text.Trim = "" Then
            MessageBox.Show("សូមបញ្ចូលទិន្ន័យមន្រ្តីផ្ទេរមកពីឱ្យបានត្រឹមត្រូវ")
            txttransfer.Focus()
            Exit Sub
        End If
        If txtaddress.Text.Trim = "" Then
            MessageBox.Show("សូមបញ្ចូលអាសយដ្ឋានបច្ចុប្បន្នរបស់មន្រ្តីឱ្យបានត្រឹមត្រូវ")
            txtaddress.Focus()
            Exit Sub
        End If

        Dim fp = ".\pdf\" & txtno.Text & "position.pdf"
        Dim fp1 = ".\pdf\" & txtno.Text & "role.pdf"
        Dim fp2 = ".\pdf\" & txtno.Text & "datein.pdf"

        'MessageBox.Show(fp2)
        Try
            If txtpathp.Text <> "" And txtval.Text = "1" Then
                
                If System.IO.File.Exists(fp) Then
                    For i = 1 To 19 Step 1
                        Dim pat = ".\pdf\" & txtno.Text & "position" & i & ".pdf"
                        If Not (System.IO.File.Exists(pat)) Then
                            My.Computer.FileSystem.MoveFile(fp, pat) 'fp & i)
                            Exit For
                        End If
                    Next
                    'System.IO.File.Delete(fp)
                End If
                My.Computer.FileSystem.CopyFile(txtpathp.Text, fp, True)
            Else
                fp = txtpathp.Text
            End If

            If txtpathr.Text <> "" And txtval2.Text = "1" Then
                
                If System.IO.File.Exists(fp1) Then
                    For i = 1 To 19 Step 1
                        Dim pat = ".\pdf\" & txtno.Text & "role" & i & ".pdf"
                        If Not (System.IO.File.Exists(pat)) Then
                            My.Computer.FileSystem.MoveFile(fp1, pat) 'fp1 & i)
                            Exit For
                        End If
                    Next
                    'System.IO.File.Delete(fp1)
                End If
                My.Computer.FileSystem.CopyFile(txtpathr.Text, fp1, True)
            Else
                fp1 = txtpathr.Text
            End If

            If txtpathdate.Text <> "" And txtval3.Text = "1" Then
                
                If System.IO.File.Exists(fp2) Then
                    For i = 1 To 19 Step 1
                        Dim pat = ".\pdf\" & txtno.Text & "datein" & i & ".pdf"
                        If Not (System.IO.File.Exists(pat)) Then
                            My.Computer.FileSystem.MoveFile(fp2, pat) 'fp2 & i)
                            Exit For
                        End If
                    Next
                    ' System.IO.File.Delete(fp2)
                End If
                My.Computer.FileSystem.CopyFile(txtpathdate.Text, fp2, True)
            Else
                fp2 = txtpathdate.Text
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

        Dim cbdate As String
        Try
            cmdp.Parameters("@empno") = txtno.Text
            cmdp.Parameters("@name") = txtname.Text
            cmdp.Parameters("@@") = cbosex.SelectedValue
            cmdp.Parameters("@dob") = dtpdob.Value
            cmdp.Parameters("@mini") = cbominis.SelectedValue
            cmdp.Parameters("@pob") = txtpob.Text
            cmdp.Parameters("@phone") = txtphone.Text
            cmdp.Parameters("@know") = txtknowledge.Text
            cmdp.Parameters("@flang") = txtlanguage.Text
            cmdp.Parameters("@pos") = cbopos.SelectedValue
            If cbpos.Checked Then
                cmdp.Parameters("@pno") = Nothing
                cmdp.Parameters("@pdate") = Nothing
            Else
                cmdp.Parameters("@pno") = txtnopos.Text
                cmdp.Parameters("@pdate") = dtppos.Value
            End If
            'cmdp.Parameters("@pno") = txtnopos.Text
            'cmdp.Parameters("@pdate") = dtppos.Value
            cmdp.Parameters("@role") = cborole.SelectedValue
            If cbrol.Checked Then
                cmdp.Parameters("@rno") = Nothing
                cmdp.Parameters("@rdate") = Nothing
            Else
                cmdp.Parameters("@rno") = txtnorole.Text
                cmdp.Parameters("@rdate") = dtprole.Value
            End If
            'cmdp.Parameters("@rno") = txtnorole.Text
            'cmdp.Parameters("@rdate") = dtprole.Value
            cmdp.Parameters("@check") = txtcheck.Text

            If cbdatein.Checked Then
                cbdate = Nothing
            Else
                cbdate = dtpdatein.Value
            End If

            cmdp.Parameters("@datein") = cbdate
            cmdp.Parameters("@transfer") = txttransfer.Text
            cmdp.Parameters("@adr") = txtaddress.Text
            cmdp.Parameters("@detail") = txtdetail.Text
            cmdp.Parameters("@pospdf") = fp
            cmdp.Parameters("@rolepdf") = fp1
            cmdp.Parameters("@datepdf") = fp2
            cmdp.Parameters("@fault") = txtfault.Text

            Dim MemStream As New MemoryStream
            Dim DataPic_Update As Byte()
            Me.PictureBox1.Image.Save(MemStream, Imaging.ImageFormat.Png)
            DataPic_Update = MemStream.GetBuffer()
            MemStream.Read(DataPic_Update, 0, MemStream.Length)
            cmdp.Parameters("@EmpPic") = DataPic_Update

            cmdp.Parameters("@id") = id
            cmdp.ExecuteNonQuery("UPDATE Employee SET EmpNo=@empno,EmpName=@name,SexID=@sex,EmpDOB=@dob,MinistryId=@mini,EmpPOB=@pob,EmpPhone=@phone,EmpKnow=@know,EmpFLang=@flang,PositionId=@pos,Pno=@pno,Pdate=@pdate,RoleId=@role,Rno=@rno,RDate=@rdate,CheckNo=@check,DateIn=@datein,Transfer=@transfer,Address=@adr,Detail=@detail,PosPdf=@postpdf,RolePdf=@rolepdf,dateinpdf=@datepdf,Fault=@fault,Photo=@EmpPic WHERE EmpId=@id")
            MessageBox.Show("កែប្រែពត៌មានមន្រ្តីជោកជ័យ")
            Me.DialogResult = Windows.Forms.DialogResult.OK
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
        
        'cmd.ExecuteNonquery("UPDATE Employee SET EmpName='" & txtname.Text & "' Where EmpId='" & id & "'")

        'MessageBox.Show("Success")

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim ofd As New OpenFileDialog
        ofd.Filter = "jpeg|*.jpg|bmp|*.bmp|all files|*.*"
        If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.PictureBox1.Image = System.Drawing.Image.FromFile(ofd.FileName)
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnppdf.Click
        If dt.Rows(0).Item(20).ToString = "" Then
            MessageBox.Show("មន្រ្តីនេះគ្មាន File Pdf ទេ")
            Exit Sub
        End If
        Dim a As String = dt.Rows(0).Item(20)
        System.Diagnostics.Process.Start(a)
    End Sub

    Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter

    End Sub

    Private Sub btnrpdf_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnrpdf.Click
        If dt.Rows(0).Item(21).ToString = "" Then
            MessageBox.Show("មន្រ្តីនេះគ្មាន File Pdf ទេ")
            Exit Sub
        End If
        Dim a As String = dt.Rows(0).Item(21)
        System.Diagnostics.Process.Start(a)
    End Sub

    Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
        cmdp.Parameters("@id") = id
        cmdp.ExecuteNonQuery("Delete FROM Employee WHERE EmpId=@id")
        MessageBox.Show("លុបទិន្នន័យមន្រ្តីបាណជោកជ័យ")
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If dt.Rows(0).Item(22).ToString = "" Then
            MessageBox.Show("មន្រ្តីនេះគ្មាន File Pdf ទេ")
            Exit Sub
        End If
        Dim a As String = dt.Rows(0).Item(22)
        System.Diagnostics.Process.Start(a)
    End Sub

    Private Sub btnpathp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnpathp.Click
        Dim ofd As New OpenFileDialog
        ofd.Filter = "pdf|*.pdf"
        If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
            txtpathp.Text = ofd.FileName
            txtval.Text = 1
        End If
    End Sub

    Private Sub btnpathr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnpathr.Click
        Dim ofd As New OpenFileDialog
        ofd.Filter = "pdf|*.pdf"
        If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
            txtpathr.Text = ofd.FileName
            txtval2.Text = 1
        End If
    End Sub

    Private Sub btnpathdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnpathdate.Click
        Dim ofd As New OpenFileDialog
        ofd.Filter = "pdf|*.pdf"
        If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
            txtpathdate.Text = ofd.FileName
            txtval3.Text = 1
        End If
    End Sub

    Private Sub cbdatein_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbdatein.CheckedChanged
        If cbdatein.Checked Then
            dtpdatein.Visible = False
        Else
            dtpdatein.Visible = True
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

        'lblpos.Visible = False
        'lblrole.Visible = False
        Button1.Visible = False
        Button2.Visible = False
        Button3.Visible = False
        Button4.Visible = False
        btnrpdf.Visible = False
        btnppdf.Visible = False
        btnadd.Visible = False
        btnpathp.Visible = False
        btnpathdate.Visible = False
        btnpathr.Visible = False
        txtname.Focus()


        Dim bmpScreenshot As Bitmap = New Bitmap(Width, Height, PixelFormat.Format32bppArgb)
        ' Create a graphics object from the bitmap  
        Dim gfxScreenshot As Graphics = Graphics.FromImage(bmpScreenshot)
        ' Take a screenshot of the entire Form1  
        gfxScreenshot.CopyFromScreen(Me.Location.X, Me.Location.Y, 0, 0, Me.Size, CopyPixelOperation.SourceCopy)
        ' Save the screenshot   
        bmpScreenshot.Save("C:\Users\Public\Documents\Police\" & txtno.Text & ".jpg", ImageFormat.Jpeg)

        'lblpos.Visible = True
        'lblrole.Visible = True
        Button1.Visible = True
        Button2.Visible = True
        Button3.Visible = True
        Button4.Visible = True
        btnrpdf.Visible = True
        btnppdf.Visible = True
        btnadd.Visible = True
        btnpathp.Visible = True
        btnpathdate.Visible = True
        btnpathr.Visible = True
        System.Diagnostics.Process.Start("C:\Users\Public\Documents\Police\" & txtno.Text & ".jpg")
    End Sub

    
    Private Sub cbpos_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbpos.CheckedChanged
        If cbpos.Checked Then
            txtnopos.Visible = False
            dtppos.Visible = False
        Else
            txtnopos.Visible = True
            dtppos.Visible = True
        End If
    End Sub

    Private Sub cbrol_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbrol.CheckedChanged
        If cbrol.Checked Then
            txtnorole.Visible = False
            dtprole.Visible = False
        Else
            txtnorole.Visible = True
            dtprole.Visible = True
        End If
    End Sub

    Private Sub btnretire_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnretire.Click

    End Sub

    
    Private Sub btnkidview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnkidview.Click
        Dim frm As New KidView
        Dim data As New DataTable
        frm.eid = id
        cmdp.Parameters("@id") = id
        data = cmdp.ExecuteDataTable("SELECT EmpName from Employee Where EmpId=@id")
        frm.lblname.Text = data.Rows(0).Item(0)
        frm.ShowDialog()
    End Sub
End Class